import Pagination from '@/components/Pagination';
import {formatData,getNodesArr} from './rule';
export default {
	data() {
		return {
			modal1:false,
            modal2:false,
            roles:[],

            roleData:{
                "name":"",
                "remark":"",
                "status":"ENABLE"
            },
            request:
            {
                "pageNum":1,
                "pageSize":10
            },
            show:false,
			modalTitle:"",
            accreditData:[],
            userRoleId:null,
		}
	},
	computed: {
		
	},
	watch:{
		
	},
	methods: {
		edit(user){
			this.modalTitle = "修改角色";
            this.modal1 = true;
            this.roleData = JSON.parse(JSON.stringify(user));
		},
		add(){
			this.modalTitle = "添加角色";
            this.modal1 = true;
            this.roleData = {
                "name":"",
                "remark":"",
                "status":"ENABLE"
            }
		},
		modalOk(){
            let url = '/user/role/create';
            if(this.modalTitle == "修改角色"){
                url = '/user/role/update';
            }
            this.$http.post(url,this.roleData)
                .then(res => {
                    if(res.data.code=='601'){
						this.$Message.success(`${this.modalTitle}成功！`);
						this.getRoles();
						return;
					}
					this.$Notice.error({
	                    title: `${this.modalTitle}失败！`,
	                    desc: res.data.message
					});
                })
		},
		accredit(role){
            this.userRoleId = role.id;
            this.modal2 = true;
            this.getAccreditData();
		},
		accreditOk(){
            let nodes = this.$refs.Tree.getCheckedNodes();
            let data = getNodesArr(nodes);
            this.$http.post('/user/role/auth',{"userRoleId":this.userRoleId,"data":data})
                .then(res => {
                    if(res.data.code=='601'){
                        this.$Message.success(`授权成功！`);
                        return;
                    }
                    this.$Notice.error({
	                    title: `授权失败！`,
	                    desc: res.data.message
					});
                })
        },
        getRoles(){
            this.$http.post('/user/role/list',this.request)
                .then(res => {
                    if(res.data.code=='601'){
                        this.show = true;
                        this.roles = res.data.data.list;
                        if(this.roles.length==0){
                            this.show = false;
							this.$Notice.error({
								desc: "查询为空"
							});
                        }
                        return;
                    }
                    this.show = false;
					this.$Notice.error({
	                    title: '查询失败',
	                    desc: res.data.message
					});
                })
                .catch((err) => {
					//请求失败的处理
					this.show = false;
					this.$Notice.error({
	                    title: '查询失败',
	                    desc: err
	                });
				})
        },
        getAccreditData(){
            this.$http.post('/user/resource/list')
                .then(res => {
                    if(res.data.code=='601'){
                        this.accreditData = formatData(res.data.data.list);
                    }
                })
        },
        loadData(item, callback){
            this.$http.post('/user/resource/find',{"parent":item.id})
                .then(res => {
                    if(res.data.code=='601'){
                        callback(formatData(res.data.data));
                    }
                })
        }
	},
	components:{
		Pagination
	},
	created() {
        this.getRoles();   
	}
}